package com.jt.service.impl;

import com.jt.pojo.User;
import com.jt.service.HttpClientService;
import com.jt.util.ObjectMapperUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @author 张琪
 * @date 2020/12/12
 * @describe
 */
@Service
public class HttpClientServiceImpl implements HttpClientService {

    @Override
    public List<User> findUserList() {
        HttpClient httpClient = HttpClients.createDefault();
        String url = "http://sso.jt.com/user/findUserList";
        HttpGet httpGet = new HttpGet(url);
        String s = null;
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                System.out.println("UserServiceImpl.findUserList");
                HttpEntity httpEntity = httpResponse.getEntity();
                s = EntityUtils.toString(httpEntity, "UTF-8");
                System.out.println(s);
            } else {
            }
        } catch (IOException e) {
            System.out.println("请求失败!");
        }

        return ObjectMapperUtil.toObject(s, List.class);
    }
}
